﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace model
{
    class MemberCommands
    {
        public Member Member
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }

        public Boat Boat
        {
            get
            {
                throw new System.NotImplementedException();
            }
            set
            {
            }
        }
    
        public void registerMember(model.Member member)
        {
            using (var db = new MemberContext())
            {
                db.Members.Add(member);
                db.SaveChanges();
            }
        }

        public void deleteMember(model.Member member)
        {
            using (var db = new MemberContext())
            {
                var deleteMember = db.Members.Find(member.memberId);
                db.Members.Remove(deleteMember);
                db.SaveChanges();
            }
        }

        public void updateMember(model.Member updatedMember)
        {
            using (var db = new MemberContext())
            {
                var original = db.Members.Find(updatedMember.memberId);

                if (original != null)
                {
                    db.Entry(original).CurrentValues.SetValues(updatedMember);
                    db.SaveChanges();
                }
            }
        }

        public List<model.Member> getAllMembers()
        {
            using (var db = new MemberContext())
            {
                return db.Members
                    .Include("BoatsList")
                    .ToList();
            }
        }

        public void addBoatToMember(model.Boat boat, model.Member member)
        {
            boat.memberId = member.memberId;
            using (var db = new MemberContext())
            {
                db.Boats.Add(boat);
                db.SaveChanges();
            }
        }
    }
}
